﻿/*
 * Created by SharpDevelop.
 * User: notmmao
 * Date: 2008-3-29
 * Time: 2:10
 * 
 */

using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace Deebug.Games.ShortBoom
{
	/// <summary>
	/// 游戏主场景
	/// </summary>
	public class Scene:UserControl
	{
		#region 字段
		System.Windows.Forms.Timer mainTimer = new System.Windows.Forms.Timer();
		
		Graphics g ;
		
		Gate gate;
		Stopwatch timer = new Stopwatch();
		float lastTime;
		float gameTime;
		float elapsedTime;
		
		#endregion
		
		#region 属性
		
		/// <summary>
		/// 当前的关卡
		/// </summary>
		public Gate CurrentGate {
			get {
				return gate;
			}
			set {
				gate = value;
			}
		}
		
		#endregion
		
		/// <summary>
		/// 默认构造函数
		/// </summary>
		public Scene()
		{
			mainTimer.Tick += new EventHandler(MainTimerOnTick);
			mainTimer.Interval = 25;
			
			
			this.Focus();
			gate=new Gate(this);
			
			this.Location = new Point(0,0);
			
			this.BackgroundImage = gate.BackImage;
			
			this.Width = BackgroundImage.Width;
			this.Height = BackgroundImage.Height;
			
			g = this.CreateGraphics();
			timer.Reset();
			mainTimer.Enabled = true;
		}
		
		/// <summary>
		/// 开始游戏
		/// </summary>
		public void StartGame(){
			if(gate.CurrentState == GameStates.Playing)
				return;
			
			timer.Start();
			lastTime = 0.0f;
			
			gate.Initialize();
			gate.CurrentState = GameStates.Playing;
		}
		
		public void UnPauseGame(){
			if(gate.CurrentState == GameStates.Playing)
				return;
			timer.Start();
			gate.CurrentState = GameStates.Playing;
		}
		
		/// <summary>
		/// 暂停游戏
		/// </summary>
		public void PauseGame(){
			if(gate.CurrentState == GameStates.Pausing)
				return;
			//mainTimer.Enabled = false;
			this.timer.Stop();
			gate.CurrentState = GameStates.Pausing;
		}
		
		void MainTimerOnTick(object o,EventArgs e){
			
			gameTime = (float)(timer.ElapsedMilliseconds / 1000.0);
			elapsedTime = gameTime - lastTime;
			lastTime = gameTime;
			
			gate.UpDate(gameTime,elapsedTime);
			gate.Render(g);
			
		}
		
		
		#region override
		
		/// <summary>
		/// 重新UserControl的OnKeyDown事件
		/// </summary>
		/// <param name="e"></param>
		protected override void OnKeyDown(KeyEventArgs e)
		{
			
			if(e.KeyData == Keys.Enter){
				if(gate.CurrentState == GameStates.GameOver){
					this.StartGame();
				}else if(gate.CurrentState == GameStates.Pausing){
					this.UnPauseGame();
				}else if(gate.CurrentState == GameStates.Playing){
					this.PauseGame();
				}
				
			}
			
			KeyBoard.KeyDown(e.KeyData);
		}
		/// <summary>
		/// 重新UserControl的OnKeyUp事件
		/// </summary>
		/// <param name="e"></param>
		protected override void OnKeyUp(KeyEventArgs e)
		{
			KeyBoard.KeyUp(e.KeyData);
		}
		/// <summary>
		/// 重新UserControl的IsInputKey事件
		/// 验证当前按下的键位是不是可以输入的；
		/// </summary>
		/// <param name="keyData"></param>
		/// <returns></returns>
		protected override bool IsInputKey(Keys keyData)
		{
			//TODO:如何屏蔽Control等按键
			return true;
		}
		#endregion
		
	}
}
